import 'dart:math';

import 'package:dsp48_v10/src/modules/eq/my_eq_controller.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../tool/assets.dart';

class EqItem extends StatefulWidget {
  int index;
  EqItem({super.key, required this.index});
  @override
  State<EqItem> createState() => _EqItemState();
}

class _EqItemState extends State<EqItem> {
  Widget buildLbl(String text, {void Function()? ontap, Color? color}) {
    return myTextButton(
        height: 40,
        width: 16,
        radius: 4,
        colors: const [],
        onTap: ontap,
        color: color,
        child: Center(
          child: Text(text, style: const TextStyle(fontSize: 10, color: Colors.black)),
        ));
  }

  @override
  Widget build(BuildContext context) {
    final MyEqController controller = Get.find<MyEqController>();
    return Obx(() {
      final sign = globalController.rxModel.value.current_ch.sign_list[widget.index];
      print('==============>>>>>.    ${sign.enable ? 'yes' : 'no'}');
      final ison = controller.default_freq_ison[widget.index];
      double value = 0;
      double min = 0;
      double max = 0;
      Color color = Colors.transparent;
      switch (globalController.eqType.value) {
        case EditEQType.isFreq:
          value = sign.freq.toDouble();
          min = sign.min_freq.toDouble();
          max = sign.max_freq.toDouble();
          color = Color_green;
          break;
        case EditEQType.isGain:
          value = sign.gain;
          min = sign.min_gain;
          max = sign.max_gain;
          color = Color_theme;
          break;
        case EditEQType.isQ:
          value = sign.Q;
          min = sign.min_Q;
          max = sign.max_Q;
          color = Color_yellow;
          break;
      }

      final sliderdata = !controller.show_sign_index_list.contains(sign.index)
          ? SliderThemeData(
              thumbColor: Color_thumbColor,
              thumbShape: const RoundSliderThumbShape(enabledThumbRadius: 8.0),
              overlayShape: const RoundSliderOverlayShape(overlayRadius: 0.0), // 设置圆点到两端的间距为 0 以减小间距
              trackHeight: 4,
              activeTrackColor: color,
              inactiveTrackColor: Colors.white)
          : SliderThemeData(
              thumbShape: SliderTextThumbShape(
                text: '7',
                thumbRadius: 8,
                thumbColor: ison ? color : Color_disEnable,
                thumbFillColor: ison ? color : Color_disEnable,
                thumbStrokeWidth: 0,
                numberColor: Colors.black,
                drawTextRotate: -pi * 3 / 2,
              ),
              overlayShape: const RoundSliderOverlayShape(overlayRadius: 0.0), // 设置圆点到两端的间距为 0 以减小间距
              trackHeight: 4,
              activeTrackColor: color,
              inactiveTrackColor: Colors.white);
      return Column(
        children: [
          //
          NumTextfield(
            color: color,
            enabled: globalController.eqType.value == EditEQType.isFreq && ison,
            decimal: 0,
            min: sign.min_freq.toDouble(),
            max: sign.max_freq.toDouble(),
            value: sign.freq.toDouble(),
            step: 1,
            onChange: (p0) => globalController.updateSignFreq(sign, double.parse(p0), isSend: true),
          ),

          ///
          const SizedBox(height: 2),

          ///
          NumTextfield(
            color: color,
            enabled: globalController.eqType.value == EditEQType.isQ && ison,
            decimal: 1,
            min: sign.min_Q,
            max: sign.max_Q,
            value: sign.Q,
            step: 0.1,
            onChange: (p0) => globalController.updateSignQ(sign, double.parse(p0), isSend: true),
          ),

          ///
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                kSlider(
                  value: value,
                  min: min,
                  max: max,
                  trackHeight: 4,
                  thumbRadius: 8,
                  dirction: Axis.horizontal,
                  enable: ison,
                  sliderThemeData: sliderdata,
                  onChanged:
                      !ison ? null : (currentValue) => globalController.updateSign(sign, currentValue.roundDouble(1)),
                  onChangedForSend: !ison ? null : (currentValue) => globalController.sendEQ(sign),
                ),

                Align(
                  alignment: Alignment.topCenter,
                  child: Padding(
                    padding: EdgeInsetsGeometry.only(top: 10),
                    child: buildLbl(ison ? 'Y' : 'N',
                        color: ison ? Color_green : Colors.transparent,
                        ontap: () => controller.updateFreqisOn(sign.index)),
                  ),
                ),

                ///
                // RotatedBox(
                //   quarterTurns: -1, // 逆时针旋转90度（文字竖直）
                //   child: Container(
                //     padding: const EdgeInsets.only(right: 10),
                //     child: Align(
                //       alignment: Alignment.topRight,
                //       child: Text(
                //         '${sign.freq}Hz',
                //         style: const TextStyle(fontSize: 10, color: Colors.black, fontWeight: FontWeight.bold),
                //         textAlign: TextAlign.end, // 对齐方式
                //       ),
                //     ),
                //   ),
                // ),
              ],
            ),
          ),

          ///
          NumTextfield(
            color: color,
            enabled: globalController.eqType.value == EditEQType.isGain && ison,
            decimal: 1,
            min: sign.min_gain,
            max: sign.max_gain,
            value: sign.gain,
            step: 0.1,
            onChange: (p0) => globalController.updateSignGain(sign, double.parse(p0), isSend: true),
          ),
        ],
      );
    });
  }
}
